//处理图片的js类

var fs = require("fs");
var imageMagickPath = fileUtils.formatFilePath(config["image.ImageMagick"]);

var IMG_CMD_JOIN_V = "convert -append {srcList} {destPath} ";
var IMG_CMD_QUALITY_50 = "convert -quality 50 {srcList} {destPath} ";
var IMG_CMD_QUALITY_20 = "convert -quality 20 {srcList} {destPath} ";
var IMG_CMD_QUALITY_75 = "convert -quality 75 {srcList} {destPath} ";


var imgop = {

    validate: function (params) {
        //格式化
        for (var i = 0; i < params.srcList.length; i++) {
            params.srcList[i] = fileUtils.formatFilePath(params.srcList[i]);
        }
        params.destPath = fileUtils.formatFilePath(params.destPath);

        //校验
        if (params.srcList.indexOf(params.destPath) != -1) {
            throw "合并文件中存在最终文件：" + destPath;
        }
    },

    //多个文件的操作
    cmdByFile: function (params) {

        // params = {
        //     cmd: IMG_CMD_JOIN_V,
        //     srcList: ['D:\\0\\a.png', 'd:\\0\\b.png'],
        //     destPath: "d:\\0\\c.png",
        //     isDeleteOld: false
        // }

        if (!params.srcList || !params.srcList.length) {
            return;
        }

        //格式化
        for (var i = 0; i < params.srcList.length; i++) {
            params.srcList[i] = fileUtils.formatFilePath(params.srcList[i]);
        }
        params.destPath = fileUtils.formatFilePath(params.destPath);


        //拼字符串
        var srcString = "";
        for (var i = 0; i < params.srcList.length; i++) {
            srcString = srcString + "  \"" + params.srcList[i] + "\"  ";
        }
        var destPath = "  \"" + params.destPath + "\"  ";

        var cmd = imageMagickPath + fileUtils.sep + params.cmd;
        cmd = cmd.replace("{srcList}", srcString);
        cmd = cmd.replace("{destPath}", destPath);
        console.log(cmd);
        //执行
        execCmd(cmd);

        //删除
        if (params.isDeleteOld) {
            for (var i = 0; i < params.srcList.length; i++) {
                new PFile(params.srcList[i]).delete();
            }
        }

    },
    //一个文件夹下的全部图片和成1个
    cmdByDirOnce: function (p) {
        // p = {
        //     cmd: IMG_CMD_JOIN_V,
        //     dirPath: 'D:\\0\\',
        //     isDeleteOld: false,
        //     cmdType: "oneByOne"
        //     //一个一个处理：oneByOne
        //     //一个目录下全部文件合成一个：allInOne
        // }


        if (p.cmdType == "oneByOne") {
            var dir = new PFile(p.dirPath);
            var srcList = dir.getImage();
            if (!srcList.length) {
                return;
            }
            for (var i = 0; i < srcList.length; i++) {
                var params = {
                    cmd: p.cmd,
                    srcList: [srcList[i]],
                    destPath: srcList[i],
                    isDeleteOld: false
                }
                this.cmdByFile(params);
            }
        } else if (p.cmdType == "allInOne") {
            var dir = new PFile(p.dirPath);
            var srcList = dir.getImage();
            if (!srcList.length) {
                return;
            }

            var ext = new PFile(srcList[0]).extName;
            var destPath = dir.path + fileUtils.sep + dir.baseName + "." + ext;
            destPath = fileUtils.formatFilePath(destPath);
            var params = {
                cmd: p.cmd,
                srcList: srcList,
                destPath: destPath,
                isDeleteOld: p.isDeleteOld
            }
            this.cmdByFile(params);
        }
    },

    //一个文件夹下的全部图片，包括子目录
    cmdByDirIncludeSon: function (p) {

        // p = {
        //     cmd: IMG_CMD_JOIN_V,
        //     dirPath: 'D:\\0\\',
        //     isDeleteOld: false,
        //     cmdType: "oneByOne"
        //     //一个一个处理：oneByOne
        //     //一个目录下全部文件合成一个：allInOne
        // }


        //先处理当前目录
        this.cmdByDirOnce(p);

        //递归处理子目录
        var sonDirPathList = new PFile(p.dirPath).getOnlyDir();
        for (var i = 0; i < sonDirPathList.length; i++) {
            var sonP = {
                cmd: p.cmd,
                dirPath: sonDirPathList[i],
                cmdType: p.cmdType,
                isDeleteOld: p.isDeleteOld
            }

            this.cmdByDirIncludeSon(sonP);
        }
    },

}